/*
 * 
 *  This file is part of Sourceprojects.org Lycia Copyright 2010 noctarius
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 * 
 */
package org.sourceprojects.lycia.test;

import static org.junit.Assert.assertEquals;
import static org.sourceprojects.lycia.fluent.FluentBuilder.dataConverter;
import static org.sourceprojects.lycia.fluent.FluentBuilder.method;
import static org.sourceprojects.lycia.fluent.FluentBuilder.prepare;
import static org.sourceprojects.lycia.fluent.FluentBuilder.validateSchema;
import static org.sourceprojects.lycia.fluent.FluentBuilder.xpath;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;
import org.sourceprojects.lycia.TypeConverter;
import org.sourceprojects.lycia.LyciaParser;
import org.sourceprojects.lycia.annotations.Attribute;
import org.sourceprojects.lycia.annotations.ContextObject;
import org.sourceprojects.lycia.fluent.FluentBuilder;

public class GlobalDataConverterTestCase {
	private static final String XML_DATA = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<matches><synset><term term=\"value1\" /><term term=\"value2\" /><term /></synset></matches>";

	@Test
	public void testXmlPathConfigure() throws Exception {
		final Method method = getClass().getMethod("parseData", List.class,
				Foo.class);

		final FluentBuilder<List<Foo>> builder = prepare();

		final LyciaParser<List<Foo>> xmlParser = builder.parser(
				xpath("//matches/synset/term[@term]", method(method, this)))
				.configure(validateSchema(false),
						dataConverter(new FooDataConverter())).build();

		xmlParser.getParserContext().setContextObject(new ArrayList<Foo>());

		xmlParser.parse(XML_DATA);
		final List<Foo> values = xmlParser.getParserContext()
				.getContextObject();

		assertEquals(2, values.size());
		assertEquals("value1", values.get(0).getValue());
		assertEquals("value2", values.get(1).getValue());
	}

	public void parseData(@ContextObject final List<Foo> values,
			@Attribute("term") final Foo value) {

		values.add(value);
	}

	private static class FooDataConverter implements TypeConverter {

		@Override
		public boolean accept(Class<?> type) {
			return type.equals(Foo.class);
		}

		@Override
		public <B> Object convert(Object value, Class<?> type,
				LyciaParser<B> parser) throws Exception {

			return new Foo((String) value);
		}

		@Override
		public Object convert(Object value, Class<?> type) throws Exception {
			return convert(value, type, null);
		}

	}

	private static class Foo {
		private String value;

		public Foo(String value) {
			this.value = value;
		}

		public String getValue() {
			return value;
		}
	}

}
